package org.azmat.demoredis.Redis.RedisTemplate;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.concurrent.TimeUnit;

@SpringBootTest
public class RedisOpsForValue {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void test1() throws InterruptedException {
        redisTemplate.opsForValue().set("Test1", "Test Timeout 5 seconds ", 5, TimeUnit.SECONDS);
        System.out.println("第 0s 取值 " + redisTemplate.opsForValue().get("Test1"));
        Thread.sleep(4000);
        System.out.println("第 4s 取值 " + redisTemplate.opsForValue().get("Test1"));
        Thread.sleep(2000);
        System.out.println("第 6s 取值 " + redisTemplate.opsForValue().get("Test1"));
    }

    @Test
    void test2() throws InterruptedException {
        redisTemplate.opsForValue().set("Test2","Test getAndSet1");  // 设置值
        System.out.println(redisTemplate.opsForValue().getAndSet("Test2", "Test getAndSet2")); // 获取值并设置新值
        System.out.println(redisTemplate.opsForValue().get("Test2")); // 获取值
    }

    @Test
    void test3() {
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.opsForValue().append("Test3", "Test append"); // 追加值
        System.out.println(redisTemplate.opsForValue().get("Test3")); // 获取值
        // 将内容追加到字符串的末尾
        redisTemplate.opsForValue().append("Test3", "OK!");
        System.out.println(redisTemplate.opsForValue().get("Test3")); // 获取值
    }

    @Test
    void test4() {
        // 新值
        redisTemplate.opsForValue().set("Test4", "Test delect");
        System.out.println(redisTemplate.opsForValue().get("Test4"));
        // 删除值
        redisTemplate.delete("Test4");
        System.out.println(redisTemplate.opsForValue().get("Test4"));
    }


}
